import { categoryListByTypeApi, getZpStreetListApi } from '@/api/common'
const state = {
  commonOptionsCategory:{ list:[], yys:[], zyws:[], ylws:[], ggws:[], map:{},},//type 类型(医疗卫生=ylws,公共卫生=ggws,yys=饮用水,zyws=职业卫生)
  commonOptionsStreet:{ list:[], map:{}},
}
const mutations = {
  SET_CATEGORY:(state, val) => {
    state.commonOptionsCategory = val
  },
  SET_STREET:(state, val) => {
    state.commonOptionsStreet = val
  },
}
const actions = {
  async getOptions({ dispatch }){
    try {
      await dispatch('getCategoryOptions')
      await dispatch('getStreetOptions')
    } catch (error) {
      console.log('error=>',error)
    }
  },
  async getStreetOptions({ commit }){
    try {
      const {code, data} = await getZpStreetListApi()
      if( code === 200 && data.length ){
        const street = { list:[], map:{},}
        data.forEach( item =>{
          item.label = item.mname
          item.value = item.code
          street.map[item.id] = item
          street.list.push(item)
        })
        commit('SET_STREET', street)
      }
    } catch (error) {
      console.log('error=>', error)
    }
  },
  async getCategoryOptions({ commit }){
    try {
      const {code, data} = await categoryListByTypeApi()
      if( code === 200 && data.length ){
        const category = { list:[], map:{},}
        data.forEach( item =>{
          item.label = item.categoryName
          item.value = item.id
          category.map[item.id] = item
          if( item.enterpriseInfoType in category === false){
            category[item.enterpriseInfoType] = []
          }
          category[item.enterpriseInfoType].push(item)
          category.list.push(item)
        })
        commit('SET_CATEGORY', category)
      }
    } catch (error) {
      console.log('error=>', error)
    }
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}